package com.ebt.m.proposal_v2.adapter;

import android.app.Activity;
import android.graphics.Color;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ebt.m.R;
import com.ebt.m.proposal_v2.base.CompatRecyclerAdapter;
import com.ebt.m.proposal_v2.base.CompatRecyclerViewHolder;
import com.ebt.m.proposal_v2.dao.request.ParamGetRisks;
import com.ebt.m.proposal_v2.dao.response.LabelInfo;
import com.ebt.m.proposal_v2.dao.response.ResponseMain;
import com.ebt.m.proposal_v2.mvp.presenter.MainItemPresenter;
import com.ebt.m.proposal_v2.utils.DateUtils;
import com.ebt.m.proposal_v2.utils.ViewUtils;
import com.ebt.m.proposal_v2.widget.callback.OnRecyclerItemClickListener;
import com.ebt.m.utils.UIHelper;

import java.util.List;

import butterknife.BindView;

/**
 * 主险产品适配器
 *
 * @author archer.qi
 *         Created on 2017/5/23.
 */

public class MainProductAdapter extends CompatRecyclerAdapter<ResponseMain, MainProductAdapter.ViewHolder> {

    public int positionCheck = -1;
    public MainItemPresenter mPresenter; // 处理点击返回
    public ParamGetRisks mParam;
    public boolean isLabelMode; // 是否是标签模式
    public boolean showBrand;   //显示品牌

    public int PADDING_HORIZONTAL;
    public int PADDING_VERTICAL;
    public final int LEFT_MARGIN = (int) (10 * ViewUtils.screenDensity(mContext));

    public MainProductAdapter(Activity context, List<ResponseMain> data, ParamGetRisks param) {
        super(context, data);
        mPresenter = new MainItemPresenter(context);
        this.mParam = param;

        // 标签参数
        PADDING_HORIZONTAL = (int) (6 * ViewUtils.screenDensity(mContext));
        PADDING_VERTICAL = (int) (3 * ViewUtils.screenDensity(mContext));
    }

    public void setLabelMode(boolean labelMode) {
        isLabelMode = labelMode;
    }

    public void showBrandLabel(boolean showBrand){
        this.showBrand = showBrand;
    }

    public void clearData() {
        mData = null;
        notifyDataSetChanged();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(inflater.inflate(R.layout.adapter_main_product, parent, false));
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        ResponseMain data = mData.get(position);
        if (data == null) {
            return;
        }
        holder.setName(data.name);
        holder.setBrandName(data.brandShortName);
        holder.setChecked(positionCheck == position);
        holder.setEnabled(data.isEnable == 1);
        holder.clickItem(position);

        holder.initLabelMode();
        holder.initShowBrand();
        holder.addLabelItems(data.tagInfo, data.isOpenInsurance);

    }


    public class ViewHolder extends CompatRecyclerViewHolder {
        @BindView(R.id.item_main_product_name)
        TextView mName;
        @BindView(R.id.item_main_product_brand)
        TextView mBrandName;
        @BindView(R.id.item_main_product_state)
        ImageView mState;
        @BindView(R.id.item_main_product_labelContainer)
        LinearLayout mLabelContainer;

        public ViewHolder(View itemView) {
            super(itemView);
        }

        public void setName(String name) {
            mName.setText(name == null ? "" : name.trim());
        }

        public void setBrandName(String name) {
            mBrandName.setText(name == null ? "" : name);
        }

        public void setChecked(boolean checked) {
            mState.setVisibility(checked ? View.VISIBLE : View.GONE);
        }

        public void setEnabled(boolean enabled) {
            mName.setEnabled(enabled);
        }

        public void clickItem(int position) {
            itemView.setOnClickListener(new ItemClick(position));
        }

        public void initLabelMode() {
            if (isLabelMode) {
                mLabelContainer.setVisibility(View.VISIBLE);
            } else {
                mLabelContainer.setVisibility(View.GONE);
            }
        }

        public void initShowBrand(){
            if (showBrand){
                mBrandName.setVisibility(View.VISIBLE);
            } else {
                mBrandName.setVisibility(View.GONE);
            }
        }

        public void addLabelItems(List<LabelInfo> labels, Integer isOpenInsurance) {
            if (!isLabelMode || mLabelContainer.getVisibility() == View.GONE) {
                return;
            }
            if (mLabelContainer.getChildCount() > 0) {
                mLabelContainer.removeAllViews();
            }
            if (labels != null && labels.size() > 0) {
                for (LabelInfo label : labels) {
                    if (label == null || (label != null && TextUtils.isEmpty(label.getTagName()))) {
                        continue;
                    }
                    if (!DateUtils.isBetween(label.getTagStartTime(), label.getTagEndTime())) {
                        continue;
                    }
                    TextView mLabelView = createLabelItemView(label.getTagName(),
                            Color.WHITE, ViewUtils.parseColor(label.getBackColor(), Color.GRAY));
                    mLabelContainer.addView(mLabelView);
                }
            }
            if (isOpenInsurance != null && isOpenInsurance > 0){
                TextView mLabelView = createLabelItemView("可投单",
                        Color.WHITE, ViewUtils.parseColor("", 0xffFDAE14));
                mLabelContainer.addView(mLabelView);
            }
        }

        private TextView createLabelItemView(String text, int solidColor, int strokeColor) {
           LinearLayout.LayoutParams PARAM = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            if (mLabelContainer.getChildCount() == 0) {
                PARAM.leftMargin = 0;
            } else {
                PARAM.leftMargin = LEFT_MARGIN;
            }
            TextView labelView = new TextView(mContext);
            labelView.setPadding(PADDING_HORIZONTAL, PADDING_VERTICAL, PADDING_HORIZONTAL, PADDING_VERTICAL);
            labelView.setGravity(Gravity.CENTER);
            ViewUtils.setBackgroundDrawable(labelView, ViewUtils.createCorneredStrokedDrawable(4, solidColor, strokeColor));
            labelView.setLayoutParams(PARAM);
            labelView.setText(text);
            labelView.setTextColor(strokeColor);
            labelView.setTextSize(10);
            return labelView;
        }

        public class ItemClick implements View.OnClickListener {
            public int position;

            public ItemClick(int pos) {
                this.position = pos;
            }

            @Override
            public void onClick(View v) {
                ResponseMain entity = mData.get(position);
                if (entity == null) {
                    return;
                }
                if (entity.isEnable == 0 && !TextUtils.isEmpty(entity.warnMsg)) {
                    UIHelper.makeToast(mContext, entity.warnMsg);
                    return;
                }
                positionCheck = position;
                notifyDataSetChanged();

                if (mPresenter != null) {
                    // 点击主险处理
                    mPresenter.onItemClick(mData, position, mParam);
                }
            }
        }
    }
}

